<!DOCTYPE html>
<html>

<head lang="en">
    <title>找攻略</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategy.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>

    <script>
        $(function () {
            //定义全局变量获取搜索框内容
            var search;
            $("#searchBtn").val('');
            //给搜索框绑定点击事件
            $("#searchBtn").click(function () {
                //获取搜索框的内容
                search=$(this).val();
            });

            //给搜索按钮绑定点击事件
            $("#buttonSearch").click(function () {
                //调用点击事件
                $("#searchBtn").click();
                console.log(search);
                if (search){
                    $.ajax({
                        url:baseUrl+"/strategies/{search}",
                        type:'post',
                        data:{search:search},
                        success:function (data) {
                            console.log(data);
                            //将除搜索外的所有标签全部隐藏起来
                            $("#pills-tab").attr("hidden",true);
                            $("#pills-tabContent").attr("hidden",true);
                            //将隐藏的div解除隐藏
                            $(".strategyHidden").attr("hidden",false);
                            //对div进行数据渲染
                            $(".strategyHidden").renderValues(data,{
                                getStrategyId:getHref
                            });
                            $(".search-head a").attr("href","");
                        }
                    });
                }
            })






            //获取攻略推荐游记
            $.get(baseUrl + "/travelCommends/3",{pageSize:1}, function (data) {
                if (data.list) {
                    $("#pills-top .commend").renderValues(data.list[0], {
                        getHref: getHref
                    });
//                    $("#pills-top .carousel-item:first").addClass("active");
                }
            });
            //获取状态为推荐攻略数据
            /**
             * 资源:/strategies
             * 请求方式:get
             * 请求参数:{state:1}
             * 返回值:List<Strategy>
             */
            $.get(baseUrl + "/strategies",{state:1}, function (data) {
                $("#pills-top .strategyCommend").renderValues({list:data}, {
                     getHref: getHref
                 });
             });
            //获取推荐的地区信息
            /**
             * 资源:/regions
             * 请求方式:get
             * 请求参数:{state:1}
             * 返回值:List<Region>
             */
            var regionId;
            $.get(baseUrl+"/regions",{state:1},function(data){
                var toolbar = "";
                var div = "";
                $.each(data, function(index,ele){
                    toolbar+='<li class="nav-item" data-id="'+ele.id+'"><a class="nav-link" data-toggle="pill" href="#pills-'+ele.id+'">'+ele.name+'</a></li>';
                    div+='<div class="tab-pane fade" id="pills-'+ele.id+'"></div>';
                });
                $("#pills-tab").append(toolbar);
                $("#pills-tabContent").append(div);
                //给地区绑定点击事件
                $(".nav-item[data-id]").click(function(){
                    regionId = $(this).data("id");
                    /**
                     * 获取指定地区的攻略数据
                     * 资源:/regions/{regionId}/strategies
                     * 请求方式:get
                     * 请求参数:regionId
                     * 返回值:PageIfo
                     */
                    $.get(baseUrl+"/regions/"+regionId+"/strategies",function(data){
                        $("#pills-"+regionId).html(data);
                    },"html");
                });
            });
            var currentPage = 2;//定义当前页,初始化为2
            var totalPage=2;//设置成2,目的是分页功能的实现
            var loading = false;//判断是否真正加载数据
            function query() {
                if (loading == true) {
                    return;
                }
                loading = true;
                $.get(baseUrl+"/regions/"+regionId+"/strategies", {currentPage: currentPage}, function (data) {
                    //console.log(data);
                    //console.log(currentPage);
                    $.each(data.list, function(index,ele){
                        //赋值模板
                        var div = $("#template .mb").clone(true);
                        $(div).find("a").attr("href",'../strategyCatalogs.html?id='+ele.id);
                        $(div).find("img").attr("src",ele.coverUrl);
                        $(div).find("span").html(ele.title);
                        $(".classify").append(div);
                    });
                    currentPage++;//当前页+1
                    totalPage = data.pages;//设置总页数
                    loading = false;
                });
            };
            $(window).scroll(function () {
                if ($(window).height() + $(document).scrollTop()+1 >= $(document).height()) {
//                    console.log($(window).height());
//                    console.log($(document).scrollTop());
//                    console.log($(document).height());
                    if (currentPage <= totalPage && regionId) {
                        query();
                    } else {
                        $(document).dialog({
                            type: 'notice',
                            infoText: '已经到底啦',
                            autoClose: 2500,
                            position: 'bottom'  // center: 居中; bottom: 底部
                        });
                    }
                }
            });
        })
    </script>

</head>

<body style="background: url('/upload/2.bmp')">

<div id="template" style="display: none;">
    <div class="col-6 mb">
        <a href="../strategyCatalogs.html?id=${a.id}">
            <img class="float-left ">
            <div class="classify-text">
                <span></span>
                <p>0人收藏</p>
            </div>
        </a>
    </div>
</div>

<div class="search-head" style="background: url('/upload/2.bmp')">
    <div class="row nav-search">
        <div class="col-1">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-10">
            <div class="input-group-sm search">
                <input class="form-control" id="searchBtn" placeholder="找攻略">
            </div>
            <a href="#">
                <i class="fa fa-search" id="buttonSearch"
                   style="color:white;font-size:25px;position: absolute;top: 23%;left: 92%">
                </i>
            </a>
        </div>
        <div class="col-1">
            <!--<button class="btn" id="buttonSearch">搜索</button>-->
        </div>
    </div>
</div>
<!--定义隐藏div,用于搜索显示-->
<div class="container strategyHidden" hidden>
    <div class="row hot" render-loop="list">
        <div class="col-12">
            <a data-url="strategyCatalogs.html?id=" render-fun="getStrategyId" render-key="list.id">
                <img render-src="list.coverUrl">
                <p render-html="list.title">成都攻略</p>
            </a>
        </div>
    </div>
</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active show" data-toggle="pill" href="#pills-top">
            <span style="font-weight: bold;">推荐</span>
        </a>
    </li>
</ul>

<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-top">
        <div class="container commend">
            <a data-url="travelContent.html?id="  render-fun="getHref" render-key="travelId">
                <img render-src="coverUrl">
                <p render-html="title">入住长隆熊猫酒店，天天都是儿童节</p>
            </a>
        </div>
        <hr>
        <div class="container strategyCommend">
            <h6>当季推荐</h6>
            <div class="row hot" render-loop="list">
                <div class="col-4">
                    <a data-url="strategyCatalogs.html?id=" render-fun="getHref" render-key="list.id">
                        <img render-src="list.coverUrl">
                        <p render-html="list.title">成都攻略</p>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>


</body>

</html>